﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using SharppiFlowEditor.controllers;

namespace SharppiFlowEditor.views
{
    public class DiagramToolBar
    {

        private AppView view;
        private AppController controller;

        private ToolStrip appToolBar;
        public ToolStrip AppToolBar
        {
            get { return appToolBar; }
        }

        private ImageList appToolBarImageList;
        private ToolStripButton addNodeToolButton;
        private ToolStripButton connectorToolButton;
        private ToolStripButton eraserToolButton;

        //uudet toolbar-namiskat
        private ToolStripButton editToolButton;
        private ToolStripLabel zoomToolLabel;
        private ToolStripComboBox zoomToolComboBox;
        public ToolStripButton snapToGridButton;
        public ToolStripComboBox snapToGridSizeComboBox;
        public ToolStripLabel snapToGridSizeLabel;

        public String ZoomZomboBoxText
        {
            get { return zoomToolComboBox.Text; }
        }

        public event ZoomChangedEventHandler ZoomChanged;
        public delegate void ZoomChangedEventHandler(object sender, EventArgs e);


        internal DiagramToolBar(AppView appView, AppController appController)
        {

            this.view = appView;
            this.controller = appController;

            appToolBar = new ToolStrip();
            appToolBar.Height = 100;
            appToolBarImageList = new ImageList();
            addNodeToolButton = new ToolStripButton();
            addNodeToolButton.Name = "ToolBarNodeButton";
            addNodeToolButton.ToolTipText = "Create nodes (CTRL + 2)";
            addNodeToolButton.Click += new EventHandler(controller.AddNodeToolButton_Click);
            connectorToolButton = new ToolStripButton();
            connectorToolButton.Name = "ToolBarConnectorButton";
            connectorToolButton.ToolTipText = "Connect nodes (CTRL + 3)";
            connectorToolButton.Click += new EventHandler(controller.ConnectorToolButton_Click);
            eraserToolButton = new ToolStripButton();
            eraserToolButton.Name = "ToolBarEraserButton";
            eraserToolButton.ToolTipText = "Delete nodes and connectors (CTRL + 4)";
            eraserToolButton.Click += new EventHandler(controller.EraserToolButton_Click);
            editToolButton = new ToolStripButton();
            editToolButton.Name = "ToolBarEditButton";
            editToolButton.ToolTipText = "Select/move nodes and connectors (CTRL + 1)";
            editToolButton.Click += new EventHandler(controller.EditToolButton_Click);

            zoomToolLabel = new ToolStripLabel();
            zoomToolLabel.Text = "Zoom: ";

            zoomToolComboBox = new ToolStripComboBox();
            for (int i = 20; i <= 200; i += 20)
            {
                zoomToolComboBox.Items.Add(i + "%");
            }
            zoomToolComboBox.MaxDropDownItems = 3;
            zoomToolComboBox.DropDownHeight = 180;
            zoomToolComboBox.SelectedIndex = 4;
            zoomToolComboBox.MergeAction = MergeAction.Insert;
            zoomToolComboBox.Size = new Size(30, 25);
            zoomToolComboBox.SelectedIndexChanged += new EventHandler(zoomToolComboBox_SelectedIndexChanged);
            zoomToolComboBox.KeyPress += new KeyPressEventHandler(zoomToolComboBox_KeyPress);

            snapToGridButton = new ToolStripButton();
            snapToGridButton.Text = "Snap to grid: off";
            snapToGridButton.Name = "ToolBarSnapToGridButton";
            snapToGridButton.Click += new EventHandler(controller.SnapToGridButton_Click);
            snapToGridButton.CheckOnClick = true;

            snapToGridSizeLabel = new ToolStripLabel();
            snapToGridSizeLabel.Text = "Grid size: ";

            snapToGridSizeComboBox = new ToolStripComboBox();
            snapToGridSizeComboBox.Items.Add("10");
            snapToGridSizeComboBox.Items.Add("20");
            snapToGridSizeComboBox.Items.Add("30");
            snapToGridSizeComboBox.MaxDropDownItems = 3;
            snapToGridSizeComboBox.DropDownHeight = 60;
            snapToGridSizeComboBox.Size = new Size(30, 25);
            snapToGridSizeComboBox.SelectedIndex = 0;
            snapToGridSizeComboBox.SelectedIndexChanged += new EventHandler(controller.SnapToGridSizeComboBox_SelectedIndexChanged);

            ///


            appToolBarImageList.ImageSize = new Size(32, 32);
            appToolBarImageList.Images.Add(Image.FromFile("img\\cursorTool.png"));
            appToolBarImageList.Images.Add(Image.FromFile("img\\nodeTool.png"));
            appToolBarImageList.Images.Add(Image.FromFile("img\\connectorTool.png"));
            appToolBarImageList.Images.Add(Image.FromFile("img\\eraserTool.png"));
            appToolBar.ImageList = appToolBarImageList;

            ToolStripSeparator separator1 = new ToolStripSeparator();
            separator1.Margin = new Padding(10, 2, 10, 2);
            ToolStripSeparator separator2 = new ToolStripSeparator();
            separator2.Margin = new Padding(10, 2, 10, 2);

            appToolBar.Items.AddRange(new ToolStripItem[] {
                editToolButton,
                addNodeToolButton,
                connectorToolButton,
                eraserToolButton,
                separator1,
                zoomToolLabel,
                zoomToolComboBox,
                separator2,
                snapToGridButton,
                snapToGridSizeLabel,
                snapToGridSizeComboBox});
            editToolButton.ImageIndex = 0;
            addNodeToolButton.ImageIndex = 1;
            connectorToolButton.ImageIndex = 2;
            eraserToolButton.ImageIndex = 3;

            appToolBar.BackColor = Color.Lavender;
            appToolBar.Padding = new Padding(5);
            appToolBar.GripStyle = ToolStripGripStyle.Hidden;
            appToolBar.ImageScalingSize = new Size(32,32);

            appToolBar.Enabled = false;
            snapToGridSizeLabel.Enabled = false;
            snapToGridSizeComboBox.Enabled = false;
        }

        public void SetEnabled(bool enable)
        {
            this.appToolBar.Enabled = enable;
        }

        void zoomToolComboBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                String text = zoomToolComboBox.Text;
                text = text.Trim();
                if (text.EndsWith("%"))
                {
                    if (zoomToolComboBox.Items.Contains(text))
                    {
                        zoomToolComboBox.SelectedItem = text;
                        ZoomChanged(this, new EventArgs());
                    }
                    else
                    {
                        String splittedText = text.Remove(text.Length - 1);
                        try
                        {
                            int zoomSize = Convert.ToInt32(splittedText);
                            if (zoomSize <= 200 && zoomSize >= 20)
                            {
                                ZoomChanged(this, new EventArgs());
                            }
                            else
                            {
                                if (zoomSize < 20)
                                {
                                    zoomToolComboBox.SelectedIndex = 0;
                                }
                                else
                                {
                                    zoomToolComboBox.SelectedIndex = zoomToolComboBox.Items.Count - 1;
                                }
                            }

                        }
                        catch (Exception ex)
                        {
                            zoomToolComboBox.SelectedItem = "100%";
                        }

                    }
                }
                else
                {
                    try
                    {
                        int zoomSize = Convert.ToInt32(text);
                        if (zoomSize <= 200 && zoomSize >= 20)
                        {
                            text = text.Insert(text.Length, "%");
                            if (zoomToolComboBox.Items.Contains(text))
                            {
                                zoomToolComboBox.SelectedItem = text;
                                ZoomChanged(this, new EventArgs());
                            }
                            else
                            {
                                zoomToolComboBox.Text = text;
                                ZoomChanged(this, new EventArgs());
                            }
                        }
                        else
                        {
                            if (zoomSize < 20)
                            {
                                zoomToolComboBox.SelectedIndex = 0;
                            }
                            else
                            {
                                zoomToolComboBox.SelectedIndex = zoomToolComboBox.Items.Count - 1;
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        zoomToolComboBox.SelectedItem = "100%";
                    }
                }
            }

        }

        void zoomToolComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        
    }
}
